package com.film.dao;

import com.film.entity.Movie;
import com.film.util.DBTemplate;
import com.film.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HustleLr
 * @version 1.0.0
 * @date 2023/12/13 14:17
 * @describe MovieDao
 */
public class MovieDao extends DBTemplate {
    @Override
    protected Object processResultSet(ResultSet rs) throws SQLException {
        List<Movie> movies = new ArrayList<>();
        while (rs.next()) {
            String movieId = rs.getString("movie_id");
            String title = rs.getString("title");
            int duration = rs.getInt("duration");
            String director = rs.getString("director");
            Movie movie = new Movie();
            movie.setMovieId(movieId);
            movie.setDirector(director);
            movie.setTitle(title);
            movie.setDuration(duration);
            movies.add(movie);
        }
        return movies;
    }

    @Override
    protected PreparedStatement createPreparedStatement(Connection conn, String sql, List<Object> params) throws SQLException {
        PreparedStatement sta = conn.prepareStatement(sql);
        if(null!=params&&!params.isEmpty()) {
            for (int i = 0; i < params.size(); i++) {
                sta.setObject(i + 1, params.get(i));
            }
        }
        return sta;
    }

    public List<Movie> selectAll() {
        String sql = "select * from movies";
        return (List<Movie>) executeQuery(sql, null);
    }
}
